package com.aliyun.sample;


import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

public class Sample {

    /**
     * 使用AK&SK初始化账号Client
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     */
    public static com.aliyun.ocr_api20210707.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填，您的 AccessKey ID
                .setAccessKeyId("LTAI5tHDAfZzt8gRgYAf85Q7")
                // 必填，您的 AccessKey Secret
                .setAccessKeySecret("fBQHtT75ksW7w3os3pVblkmYSa1IgB");
        config.endpoint = "ocr-api.cn-hangzhou.aliyuncs.com";
        return new com.aliyun.ocr_api20210707.Client(config);
    }

    /**
    * 使用STS鉴权方式初始化账号Client，推荐此方式。
    * @param accessKeyId
    * @param accessKeySecret
    * @param securityToken
    * @return Client
    * @throws Exception
    */
    public static com.aliyun.ocr_api20210707.Client createClientWithSTS(String accessKeyId, String accessKeySecret, String securityToken) throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填，您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 必填，您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret)
                // 必填，您的 Security Token
                .setSecurityToken(securityToken)
                // 必填，表明使用 STS 方式
                .setType("sts");
        config.endpoint = "ocr-api.cn-hangzhou.aliyuncs.com";
        return new com.aliyun.ocr_api20210707.Client(config);
    }

    public static void main(String[] args_) throws Exception {
        java.util.List<String> args = java.util.Arrays.asList(args_);
        com.aliyun.ocr_api20210707.Client client = Sample.createClient(System.getenv("LTAI5tHDAfZzt8gRgYAf85Q7"), System.getenv("fBQHtT75ksW7w3os3pVblkmYSa1IgB"));
        // 2.服务器上文件夹不存在则创建自动传建文件夹。
        File file = new File("D:\\aaaaaa");
        if (!file.exists() && !file.isDirectory()) {
            file.setExecutable(true);
            file.setReadable(true);
            file.setWritable(true);
            file.mkdirs();
        }
        // 3.去文件服务器上下载文件到本地，fileUrl为文件存放的原始地址;filepath为将文件从远程下载保存在本地服务器的地址，注意filepath一定要是具体文件夹地址+文件名称的，只写文件夹名称就会报错
        downloadNet("http://10.62.210.57:30118/yunli/third/v1/api/file/download/2023-08-01_094837_1690973837823.png", "D:\\aaaaaa\\2023-08-01_094837_1690973837823.png");
        java.io.InputStream bodyStream = com.aliyun.darabonba.stream.Client.readFromFilePath("D:\\aaaaaa\\2023-08-01_094837_1690973837823.png");
        com.aliyun.ocr_api20210707.models.RecognizeTableOcrRequest recognizeTableOcrRequest = new com.aliyun.ocr_api20210707.models.RecognizeTableOcrRequest()
                .setBody(bodyStream)
                .setNeedRotate(true)
                .setLineLess(true)
                .setIsHandWriting("false")
                .setSkipDetection(true);
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        com.aliyun.ocr_api20210707.models.RecognizeTableOcrResponse resp = client.recognizeTableOcrWithOptions(recognizeTableOcrRequest, runtime);
        com.aliyun.teaconsole.Client.log(com.aliyun.teautil.Common.toJSONString(resp));
    }

    public static void downloadNet(String fileUrl, String filepath) throws Exception {
        // 下载网络文件
        int byteRead = 0;
        try {
            URL url = new URL(fileUrl.replaceAll(" ","%"));
            URLConnection conn = url.openConnection();
            URLEncoder.encode("fileUrl","utf-8");
            conn.setRequestProperty("Content-Type", "utf-8");
            try (
                    InputStream inStream = conn.getInputStream();
                    FileOutputStream fs = new FileOutputStream(filepath);
            ) {
                byte[] buffer = new byte[1204];
                while ((byteRead = inStream.read(buffer)) != -1) {
                    fs.write(buffer, 0, byteRead);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception("下载证件照失败");
        }
    }
}
